layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'laytpl', 'laydate'], function(){
    var $ = layui.$,
        table = layui.table,
        laydate = layui.laydate;

    var h = getQueryVariable('height');
    if(h != ''){
        globalHeight = h;
    }

    laydate.render({
        elem: '#time', //指定元素
        type: 'datetime',
        range: true
    });

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'operate/im/list',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '消息列表',
        height: globalHeight,
        page: true,
        cols: [[
            {type: 'checkbox'},
            {field: 'id', title: 'ID', align: 'center'},
            {field: 'msgId', title: '消息标识', align: 'center', hide: true},
            {field: 'fromUserId', title: '发送者id', align: 'center'},
            {field: 'toUserId', title: '接收者id', align: 'center'},
            {field: 'type', title: '消息类型', align: 'center', templet: '#typeTpl'},
            {field: 'msg', title: '消息内容', align: 'center', templet:
                function (d) {
                    var str = d.msg.toUpperCase();
                    if(d.type == 'PICTURE'){
                        str = '' + ossDomain + '<img src="/" style="width: 100%;">' + d.msg + '';
                    }else if(d.type == 'CUSTOM'){
                        if(d.attach.type == 743){
                            str = '' + ossDomain + '<img src="/" style="width: 100%;">' + d.msg + '';
                        }else{
                            str = '自定义表情，无法显示';
                        }
                    }else if(d.type == 'AUDIO') {
                        str = '<audio controls src="' + d.msg + '" style="width: 98%;"/>';
                    }else if(d.type == 'TEXT') {
                        if(d.keywords !=''){
                            var keywordArr = d.keywords.split(",");

                            $.each(keywordArr, function (index, ele){
                                str = str.replace(ele, '<span style="color: red">' +ele + '</span>');
                            });
                        }
                        str = '<span style="font-family: auto">' + str + '</span>';
                    }
                    return str;
                }
            },
            {field: 'status', title: '状态', align: 'center', templet: '#statusTpl'},
            {field: 'createTime', title: '发送时间', align: 'center'},
            {field: 'op', title: '操作', align: 'center', templet: '#data-table-bar'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.imLogs //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        var checks = table.checkStatus(obj.config.id);
        var check = checks.data;
        switch(obj.event){
            case 'search':
                var data = getData();
                table.reload('data-table', {
                    url: 'operate/im/list',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
            case 'updateStatus':
                if(check.length <= 0){
                    layer.msg("请选择要处理的消息！");
                    return;
                }
                layer.confirm('确定处理'+check.length+'个消息', {offset: '30%'}, function(index){
                    var ids = getIds(check, 'id');
                    // 加载层
                    var load = layerLoad();
                    post('operate/im/updateStatus', {'ids': ids}, false, function (data) {
                        table.reload('data-table');
                        layer.msg("处理成功");
                        layer.close(index);
                        layer.close(load);
                    });
                });
                break;
        };
    });

    //监听行工具事件
    table.on('tool(data-table)', function(obj){
        var data = obj.data;
        if(obj.event === 'updateStatus'){
            layer.confirm('确定处理该消息', {offset: '30%'}, function(index){
                var ids = [];
                ids.push(data.id);
                // 加载层
                var load = layerLoad();
                post('operate/im/updateStatus', {'ids': ids}, false, function (data) {
                    table.reload('data-table');
                    layer.msg("处理成功");
                    layer.close(index);
                    layer.close(load);
                });
            });
        }else if(obj.event === 'view'){
            layer.open({
                type: 2,
                title: '聊天记录',
                content: 'toViewUserImLog?fromUserId='+data.fromUserId+'&toUserId='+data.toUserId,
                area: ['90%', '90%'],
                offset: ['5%', '5%'],
                end: function (){
                    table.reload('data-table');
                }
            });
        }
    });

    /**
     * 获取查询数据
     */
    function getData() {
        var fromUserId;
        if($('#fromUserId').length == 0){
            fromUserId = $($('#data-table-toolbar ').html()).find('#fromUserId').val();
        }else{
            fromUserId = $('#fromUserId').val();
        }
        var toUserId = $('#toUserId').val();
        var type = $('#type').val();
        var time = $('#time').val();

        return {'fromUserId': fromUserId, 'toUserId': toUserId, 'type': type, 'createTime': time};
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#fromUserId').val(data.fromUserId);
        $('#toUserId').val(data.toUserId);
        $('#type').val(data.type);
        $('#time').val(data.createTime);
        laydate.render({
            elem: '#time', //指定元素
            type: 'datetime',
            range: true
        });
    }
});